﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using ELGGAPI.Models;
using MySql.Data.MySqlClient;

namespace ELGGAPI.Data
{
    public class AutheticationToken
    {
        public int id { get; set; }
        public string access_token { get; set; }
        public int ticks { get; set; }
        public string username { get; set; }
        public string displayname { get; set; }
        public static AutheticationToken Authenticate(string UserName, string Password, int UserType)
        {
            string connectionString = APIHelper.ConnectionString;
            int TickCount = APIHelper.Ticks;
            int recCount = 0;
            AutheticationToken AuthToken = new AutheticationToken();
            using (MySqlConnection con = new MySqlConnection(connectionString))
            {
                MySqlCommand cmd = new MySqlCommand(APIHelper.DBName + "Auth_Validation", con);

                // Configure command and add parameters.
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new MySqlParameter("pUserName", UserName));
                cmd.Parameters.Add(new MySqlParameter("pPassword", Password));
                cmd.Parameters.Add(new MySqlParameter("pTicks", TickCount));
                cmd.Parameters.Add(new MySqlParameter("pUserType", UserType));
                // Execute the command.
                cmd.Connection.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                //dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    recCount++;
                    AuthToken.id = int.Parse(dr["UserId"].ToString());
                    AuthToken.access_token = dr["Tocken"].ToString();
                    AuthToken.ticks = int.Parse(dr["Ticks"].ToString());
                    AuthToken.username = dr["UserName"].ToString();
                    AuthToken.displayname = dr["Name"].ToString();
                }
                dr.Close();
                con.Close();
            }
            if (recCount > 0)
                return AuthToken;
            else
                return null;
        }
        public static AutheticationToken ValidateToken(string Auth_Token)
        {


            string connectionString = APIHelper.ConnectionString;
            int TickCount = APIHelper.Ticks;
            int recCount = 0;
            AutheticationToken AuthToken = new AutheticationToken();
            using (MySqlConnection con = new MySqlConnection(connectionString))
            {
                MySqlCommand cmd = new MySqlCommand(APIHelper.DBName + "Auth_Token", con);

                // Configure command and add parameters.
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new MySqlParameter("pToken", Auth_Token));
                // Execute the command.
                cmd.Connection.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                //dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    recCount++;
                    AuthToken.id = int.Parse(dr["UserId"].ToString());
                    AuthToken.access_token = dr["Tocken"].ToString();
                    AuthToken.ticks = int.Parse(dr["RemTicks"].ToString());
                    AuthToken.username = dr["UserName"].ToString();
                    AuthToken.displayname = dr["Name"].ToString();
                }
                dr.Close();
                con.Close();
            }
            if (recCount > 0)
                return AuthToken;
            else
                return null;
        }
    }
}